import { localStorage, sessionStorage } from 'mp-storage'

class Cache {
  setItem(key, value) {
    return localStorage.setItem(key, JSON.stringify(value))
  }
  getItem(key) {
    const value = localStorage.getItem(key)
    if (value) {
      return JSON.parse(value)
    }
  }
  // 删除
  deleteItem(key) {
    localStorage.removeItem(key)
  }
  // 清空
  clear() {
    localStorage.clear()
  }
  setSessionItem(key, value) {
    return sessionStorage.setItem(key, JSON.stringify(value))
  }
  getSessionItem(key) {
    const value = sessionStorage.getItem(key)
    if (value) {
      return JSON.parse(value)
    }
  }
  // 删除
  deleteSessionItem(key) {
    sessionStorage.removeItem(key)
  }
  // 清空
  clearSession() {
    sessionStorage.clear()
  }
}

const storage = new Cache()

export default app => {
  app.prototype.$storage = storage
}

export { storage }
